﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessModel;
using EntityDataModel;
using System.ComponentModel;
using System.Data.Objects.DataClasses;
using BaseUtilities;

namespace Controller.DataProvider
{
    public class SaleOrderPro : GenericProvider<SaleOrder>
    {
        public SaleOrderPro()
        {
            EntityName = "Tbl_SaleOrder";
        }

        #region override Methods
        //----------------------------------------
        public override EntityObject CreateEntity()
        {
            return new Tbl_SaleOrder();
        }
        //----------------------------------------
        public override SaleOrder CreateModel()
        {
            return new SaleOrder();
        }
        //----------------------------------------
        public override void ToEntity(SaleOrder pModel, EntityObject pEntity, params EnumFieldCopy[] pEnumFieldCopys)
        {
            Tbl_SaleOrder oEntity = pEntity as Tbl_SaleOrder;
            oEntity.SalerId = pModel.SalerId;
            oEntity.SalerUsername = pModel.SalerUsername;
            pEnumFieldCopys.ToList().ForEach((item) =>
            {
                switch (item)
                {
                    case EnumFieldCopy.Id:
                        oEntity.Id = pModel.Id;
                        break;
                    case EnumFieldCopy.Created:
                        oEntity.CreatedBy = pModel.CreatedBy;
                        oEntity.CreatedDate = pModel.CreatedDate;
                        break;
                    case EnumFieldCopy.Childs:
                        SaleOrderDetailPro detailPro = new SaleOrderDetailPro();
                        pModel.SaleOrderDetails.ToList().ForEach((child) =>
                        {
                            Tbl_SaleOrderDetail detail = oEntity.Tbl_SaleOrderDetail.Where(p => p.Id == child.Id).FirstOrDefault();
                            if (detail == null)
                            {
                                detail = new Tbl_SaleOrderDetail();
                                oEntity.Tbl_SaleOrderDetail.Add(detail);
                            }
                            detailPro.ToEntity(child, detail, EnumFieldCopy.All);
                        });
                        break;
                    case EnumFieldCopy.All:
                        oEntity.Id = pModel.Id;
                        oEntity.CreatedBy = pModel.CreatedBy;
                        oEntity.CreatedDate = pModel.CreatedDate;
                        break;
                }
            });
        }
        //----------------------------------------
        public override void ToModel(EntityObject pEntity, SaleOrder pModel, params EnumFieldCopy[] pEnumFieldCopys)
        {
            Tbl_SaleOrder oEntity = pEntity as Tbl_SaleOrder;
            pModel.SalerId = oEntity.SalerId;
            pModel.SalerUsername = oEntity.SalerUsername;
            pEnumFieldCopys.ToList().ForEach((item) =>
            {
                switch (item)
                {
                    case EnumFieldCopy.Id:
                        pModel.Id = oEntity.Id;
                        break;
                    case EnumFieldCopy.Created:
                        pModel.CreatedBy = oEntity.CreatedBy;
                        pModel.CreatedDate = oEntity.CreatedDate;
                        break;
                    case EnumFieldCopy.Childs:
                        SaleOrderDetailPro detailPro = new SaleOrderDetailPro();
                        oEntity.Tbl_SaleOrderDetail.ToList().ForEach((child) =>
                        {
                            SaleOrderDetail detail = pModel.SaleOrderDetails.Where(p => p.Id == child.Id).FirstOrDefault();
                            if (detail == null)
                            {
                                detail = new SaleOrderDetail();
                                pModel.SaleOrderDetails.Add(detail);
                            }
                            detailPro.ToModel(child, detail, EnumFieldCopy.All);
                        });
                        break;
                    case EnumFieldCopy.All:
                        pModel.Id = oEntity.Id;
                        pModel.CreatedBy = oEntity.CreatedBy;
                        pModel.CreatedDate = oEntity.CreatedDate;
                        break;
                }
            });
        }
        #endregion override Methods
        //----------------------------------------
        public ResultModel SaveSaleOrderFull(SaleOrder pSaleOrder)
        {
            ResultModel oResultModel = new ResultModel();
            oResultModel.HasError = true;
            using (ManageStoreEntities entities = new ManageStoreEntities())
            {
                Tbl_SaleOrder oEntity = entities.Tbl_SaleOrder.Where(p => p.Id == pSaleOrder.Id).FirstOrDefault();
                if (oEntity == null)
                {
                    oEntity = new Tbl_SaleOrder();
                    ToEntity(pSaleOrder, oEntity, EnumFieldCopy.Created, EnumFieldCopy.Modified);
                    entities.AddObject(EntityName, oEntity);
                }
                else
                {
                    ToEntity(pSaleOrder, oEntity, EnumFieldCopy.Modified);
                }
                // OrderDeatail.
                SaleOrderDetailPro oSaleOrderDetailPro = new SaleOrderDetailPro();
                ProductPro oProductPro = new ProductPro();
                pSaleOrder.SaleOrderDetails.ToList().ForEach((child) =>
                {
                    // Product.
                    Tbl_Product oProduct = entities.Tbl_Product.Where(p => p.Id == child.ProductId).FirstOrDefault();
                    if (oProduct != null)
                    {
                        oProduct.Quantity -= child.Quantity;
                    }
                    // SaleOrderDetail.
                    Tbl_SaleOrderDetail oDetail = entities.Tbl_SaleOrderDetail.Where(p => p.Id == child.Id).FirstOrDefault();
                    if (oDetail == null)
                    {
                        oDetail = new Tbl_SaleOrderDetail();
                        oSaleOrderDetailPro.ToEntity(child, oDetail, EnumFieldCopy.Created, EnumFieldCopy.Modified);
                        oDetail.Tbl_SaleOrder = oEntity;
                    }
                    else
                    {
                        oSaleOrderDetailPro.ToEntity(child, oDetail, EnumFieldCopy.Modified);
                    }
                });
                entities.SaveChanges();
                oResultModel.HasError = false;
                oResultModel.Result = oEntity.EntityKey.EntityKeyValues.Single().Value;
            }
            return oResultModel;
        }
    }
}
